package com.base.themvx.net

import com.base.themvx.defNonSyncLazy
import com.base.themvx.utils.XCoroutineScope
import okhttp3.OkHttpClient

/**
 ******************(^_^)***********************<br>
 * Author: fee(QQ/WeiXin:1176610771)<br>
 * Time: 16:08<br>
 * <P>DESC:
 *  统一的配置　OkhttpClient (懒汉单例)
 * </p>
 * ******************(^_^)***********************
 */
class RetrofitClient private constructor(){

    companion object Helper {
        private val mRetrofitClient by defNonSyncLazy {
            RetrofitClient()
        }

        fun build(): OkHttpClient.Builder = mRetrofitClient.mOkHttpClientBuilder


        fun getOkHttpClient() = mRetrofitClient.buildOkHttpClient()

    }

    private lateinit var mOkHttpClient: OkHttpClient
    private var mIsInitClient = false;

    private val mOkHttpClientBuilder by defNonSyncLazy {
        OkHttpClient.Builder()
    }

    fun buildOkHttpClient(): OkHttpClient {
        if (!mIsInitClient){
            mIsInitClient = true
            mOkHttpClient = mOkHttpClientBuilder.build()
            val dispatcher = mOkHttpClient.dispatcher
            XCoroutineScope.mExecutor = dispatcher.executorService //项目中复用统一的一个线程池
        }
        return mOkHttpClient
    }

}